import { createRouter, createWebHistory } from 'vue-router'

const routes = [
  {
    path: '/login',
    name: 'Login',
    component: () => import('../views/login/index.vue'),
    meta: { title: '登录', requiresAuth: false }
  },
  {
    path: '/',
    component: () => import('../components/layout/AppLayout.vue'),
    redirect: '/dashboard',
    children: [
      {
        path: 'dashboard',
        name: 'Dashboard',
        component: () => import('../views/dashboard/index.vue'),
        meta: { title: '仪表盘', icon: 'dashboard', affix: true }
      },
      {
        path: 'analytics',
        name: 'Analytics',
        component: () => import('../views/analytics/index.vue'),
        meta: { title: '数据分析', icon: 'chart-line' }
      },
      {
        path: 'message',
        name: 'Message',
        component: () => import('../views/message/index.vue'),
        meta: { title: '消息中心', icon: 'message' }
      },
      {
        path: 'profile',
        name: 'Profile',
        component: () => import('../views/profile/index.vue'),
        meta: { title: '个人中心', icon: 'user', hideInMenu: true }
      },
      {
        path: 'settings',
        name: 'Settings',
        component: () => import('../views/settings/index.vue'),
        meta: { title: '账号设置', icon: 'setting', hideInMenu: true }
      },
      {
        path: 'system',
        name: 'System',
        component: () => import('../components/layout/RouterView.vue'),
        redirect: '/system/user',
        meta: { title: '系统管理', icon: 'setting' },
        children: [
          {
            path: 'user',
            name: 'User',
            component: () => import('../views/system/user/index.vue'),
            meta: { title: '用户管理', icon: 'user' }
          },
          {
            path: 'role',
            name: 'Role',
            component: () => import('../views/system/role/index.vue'),
            meta: { title: '角色管理', icon: 'user-group' }
          },
          {
            path: 'menu',
            name: 'Menu',
            component: () => import('../views/system/menu/index.vue'),
            meta: { title: '菜单管理', icon: 'menu' }
          },
          {
            path: 'dept',
            name: 'Department',
            component: () => import('../views/system/dept/index.vue'),
            meta: { title: '部门管理', icon: 'dept' }
          },
          {
            path: 'post',
            name: 'Post',
            component: () => import('../views/system/post/index.vue'),
            meta: { title: '岗位管理', icon: 'post' }
          },
          {
            path: 'config',
            name: 'Config',
            component: () => import('../views/system/config/index.vue'),
            meta: { title: '系统设置', icon: 'config' }
          },
          {
            path: 'notice',
            name: 'Notice',
            component: () => import('../views/system/notice/index.vue'),
            meta: { title: '通知公告', icon: 'notice' }
          },
          {
            path: 'backup',
            name: 'Backup',
            component: () => import('../views/system/backup/index.vue'),
            meta: { title: '数据备份与恢复', icon: 'backup' }
          }
        ]
      },
      {
        path: 'monitor',
        name: 'Monitor',
        component: () => import('../components/layout/RouterView.vue'),
        redirect: '/monitor/log',
        meta: { title: '系统监控', icon: 'monitor' },
        children: [
          {
            path: 'log',
            name: 'Log',
            component: () => import('../views/monitor/log/index.vue'),
            meta: { title: '操作日志', icon: 'log' }
          },
          {
            path: 'login-log',
            name: 'LoginLog',
            component: () => import('../views/monitor/login-log/index.vue'),
            meta: { title: '登录日志', icon: 'login-log' }
          },
          {
            path: 'job',
            name: 'Job',
            component: () => import('../views/monitor/job/index.vue'),
            meta: { title: '定时任务', icon: 'job' }
          },
          {
            path: 'server',
            name: 'Server',
            component: () => import('../views/monitor/server/index.vue'),
            meta: { title: '服务器监控', icon: 'server' }
          },
          {
            path: 'cache',
            name: 'Cache',
            component: () => import('../views/system/cache/index.vue'),
            meta: { title: '缓存监控', icon: 'redis' }
          }
        ]
      },
      {
        path: 'tool',
        name: 'Tool',
        component: () => import('../components/layout/RouterView.vue'),
        redirect: '/tool/dict',
        meta: { title: '系统工具', icon: 'tool' },
        children: [
          {
            path: 'dict',
            name: 'Dict',
            component: () => import('../views/tool/dict/index.vue'),
            meta: { title: '数据字典', icon: 'dict' }
          },
          {
            path: 'file',
            name: 'File',
            component: () => import('../views/tool/file/index.vue'),
            meta: { title: '文件管理', icon: 'file' }
          },
          {
            path: 'api-doc',
            name: 'ApiDoc',
            component: () => import('../views/tool/apiDoc/index.vue'),
            meta: { title: 'API文档', icon: 'document' }
          }
        ]
      },
      {
        path: 'help',
        name: 'Help',
        component: () => import('../components/layout/RouterView.vue'),
        redirect: '/help/manual',
        meta: { title: '帮助中心', icon: 'help' },
        children: [
          {
            path: 'manual',
            name: 'Manual',
            component: () => import('../views/help/manual/index.vue'),
            meta: { title: '使用手册', icon: 'book' }
          },
          {
            path: 'faq',
            name: 'FAQ',
            component: () => import('../views/help/faq/index.vue'),
            meta: { title: '常见问题', icon: 'question' }
          }
        ]
      }
    ]
  },
  {
    path: '/:pathMatch(.*)*',
    name: 'NotFound',
    component: () => import('../views/error/404.vue')
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

// 路由守卫
router.beforeEach((to, from, next) => {
  // 设置页面标题
  document.title = to.meta.title ? `${to.meta.title} - 通用后台管理系统` : '通用后台管理系统'
  
  // 判断是否需要登录权限
  if (to.matched.some(record => record.meta.requiresAuth !== false)) {
    // 这里应该判断用户是否已登录，暂时简单处理
    const token = localStorage.getItem('token')
    if (!token && to.path !== '/login') {
      next({ path: '/login' })
    } else {
      next()
    }
  } else {
    next()
  }
})

export default router 